<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */
echo '<br /><h2><a href="index.php?p=components&name=user">'.Lang::getBackGlobal('back').'</a></h2><br />';
if (!empty($_POST['display_name']) && !empty($_POST['username'])) {

    if (!empty($_POST['new_pass'])) {
        $hash = new Hash($_POST['username'], $_POST['new_pass']);
        $hash->hash();
        $password = $hash->password;
        $salt = $hash->salt;
    } else {
        $password = $_POST['password'];
    }

    $username = Registry::get('db')->real_escape_string($_POST['username']);
    $display_email = (intval($_POST['display_email']) == 0) ? '0' : '1';
    $display_name = Registry::get('db')->real_escape_string($_POST['display_name']);
    $email = Registry::get('db')->real_escape_string($_POST['email']);
    $birthday = intval($_POST['year']) . '-' . intval($_POST['month']) . '-' .
        intval($_POST['day']);
    $website = Registry::get('db')->real_escape_string($_POST['website']);
    
    $update = "UPDATE {prefix}users SET `username` = '" . $username .
        "', `password` = '" . $password . "', `salt` = '" . $salt .
        "', `display_name` = '" . $display_name . "', `email` = '" . $email .
        "', `birthday` = '" . $birthday . "', `rank` = '" . intval($_POST['rank']) .
        "', `group` = '" . intval($_POST['group']) . "', `website` = '" . $website .
        "', `display_email` = '" . $display_email . "' WHERE id = " . intval($_GET['id']);
    $query = Registry::get('db')->query($update);
    
    $log->write(8, 'com_user', 'NOW()', 'User edited account #' . intval($_GET['id']),
        $_SESSION['id']);
    
    messageBox( Lang::getFront('com_user', 'edit_success') );
}
$query = Registry::get('db')->query("SELECT * FROM {prefux}users WHERE id = '" . intval($_GET['id']) . "' LIMIT 1");
$user = $query->fetch_array();
unset($query);
echo '<form action="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] .
    '" method="post"><table>';
$display_email = ($user['display_email'] == 0) ? '' : 'checked="checked"';

$groups = Registry::get('db')->query("SELECT id, name FROM {prefix}users_groups");

$groups_array = array();
for ($i = 1; $data = $groups->fetch_array(); $i++) {
    if ($i == 1)
        $groups_array[0] = Lang::getBack('com_user', 'group_none');
    $groups_array[$data['id']] = $data['name'];
}

echo '<tr><td>' . Lang::getFront('com_user', 'username') .
    ' : </td><td><input type="text" name="username" size="25" value="' . $user['username'] .
    '" disabled="disable" /></td></tr>

<tr><td>' . Lang::getFront('com_user', 'display_name') .
    ' : </td><td><input type="text" name="display_name" size="30" value="' . $user['display_name'] .
    '" /></td></tr>
<tr><td>' . Lang::getFront('com_user', 'new_pass') .
    ' : </td><td><input type="password" name="new_pass" size="30" /></td></tr>
<tr><td>' . Lang::getFront('com_user', 'email') .
    ' : </td><td><input type="text" name="email" size="30" value="' . $user['email'] .
    '" /></td></tr>
<tr><td>'.Lang::getFront('com_user', 'display_email').' : </td><td><input type="checkbox" ' . $display_email .
        ' name="display_email" value="1"></td></tr>

<tr><td>' . Lang::getFront('com_user', 'birthday') . ' : </td><td>
<select name="day" size="1">';
$birthday = explode("-", $user['birthday']);
for ($i = 1; $i <= 31; $i++) {
    $selected = ($birthday[2] == $i) ? 'selected="selected"' : '';
    echo '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
}
echo '
</select>
<select name="month" size="1">';
for ($i = 1; $i <= 12; $i++) {
    $selected = ($birthday[1] == $i) ? 'selected="selected"' : '';
    echo '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
}
echo '
</select>
<select name="year" size="1">';
for ($i = 1910; $i <= (date("Y") - 5); $i++) {
    $selected = ($birthday[0] == $i) ? 'selected="selected"' : '';
    echo '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
}
echo '
</select>
</td></tr>
<tr><td>' . Lang::getFront('com_user', 'fullname') .
    ' : </td><td><input type="text" name="real_name" size="30" value="' . $user['real_name'] .
    '" /></td></tr>
<tr><td>' . Lang::getFront('com_user', 'website') .
    ' : </td><td><input type="text" name="website" size="30" value="' . $user['website'] .
    '" /></td></tr>

<tr><td>' . Lang::getFront('com_user', 'rank') .
    ' : </td><td><select name="rank" size="1">
';
foreach ($ranks as $key => $val) {
    $selected = ((int)$user['rank'] == (int)$val) ? 'selected="selected"' : '';
    echo '<option value="' . $val . '" ' . $selected . '>' . $key . '</option>';
}
echo '
</select></td></tr>

<tr><td>' . Lang::getFront('com_user', 'group_title') .
    ' : </td><td><select name="group" size="1">
';
foreach ($groups_array as $key => $val) {
    $selected = ((int)$user['group'] == (int)$key) ? 'selected="selected"' : '';
    echo '<option value="' . $key . '" ' . $selected . '>' . $val . '</option>';
}
echo '
</select></td></tr>
<input type="hidden" name="password" value="' . $user['password'] . '" />
<tr><td><input type="submit" value="Odeslat" /></td></tr>

</table></form>
';
?>